package logic

import (
	"context"
	"user/moder/mysql"

	"user/userrpc/internal/svc"
	"user/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *userrpc.GoodsListRequest) (*userrpc.GoodsListResponse, error) {
	// todo: add your logic here and delete this line
	var goods []mysql.Goods
	mysql.DB.Table("users").
		Select("goods.goods_name",
			"goods.goods_price",
			"goods.goods_num",
			"users.id as user_id").
		Joins("LEFT JOIN goods ON goods.user_id = users.id").Find(&goods)

	var userList []*userrpc.GoodsList
	for _, good := range goods {
		list := userrpc.GoodsList{
			Goodsname:  good.GoodsName,
			Goodsprice: good.GoodsPrice,
			Goodsnum:   int64(good.GoodsNum),
			UserId:     int64(good.UserId),
		}
		userList = append(userList, &list)
	}
	return &userrpc.GoodsListResponse{
		List: userList,
	}, nil
}
